package cn.chendd.cofig.filters;

import com.google.common.collect.Lists;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * 请求参数分析过滤器
 *
 * @author chendd
 * @date 2022/12/27 21:59
 */
@Component
public class HttpParamScopeGatewayFilterFactory extends AbstractGatewayFilterFactory<HttpParamScopeGatewayFilterFactory.Config> implements Ordered {

    public HttpParamScopeGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(HttpParamScopeGatewayFilterFactory.Config config) {
        //局部过滤器，获取配置的日志参数范围
        return (exchange, chain) -> chain.filter(exchange).doFinally(item -> {

            exchange.getAttributes().put(GatewayContext.HTTP_PARAM_SCOPE , config.getScopes());
        });
    }

    @Override
    public int getOrder() {
        return -90;
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Lists.newArrayList("scopes");
    }

    @Override
    public ShortcutType shortcutType() {
        return ShortcutType.GATHER_LIST;
    }

    @Validated
    public static class Config {

        /**
         * 参数范围枚举集合
         */
        private List<String> scopes;

        public List<String> getScopes() {
            return scopes;
        }

        public void setScopes(List<String> scopes) {
            this.scopes = scopes;
        }
    }

}
